package com.inspur.edp.bef.bemanager.codegenerator.genutils;

import com.inspur.edp.bef.bemanager.generatecomponent.ComponentGenUtil;
import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bef.bizentity.GspBusinessEntity;
import com.inspur.edp.bef.bizentity.operation.collection.BizMgrActionCollection;
import com.inspur.edp.caf.generator.baseInfo.CompilationUnitInfo;
import com.inspur.edp.caf.generator.compileunit.CompileUnitGenerator;
import com.inspur.edp.caf.generator.compileunit.CompileUnitGeneratorContext;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.lcm.metadata.api.entity.ProcessMode;
import com.inspur.edp.lcm.metadata.api.service.MetadataProjectService;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.io.File;

public class BeCommonUtilsGenerator {
    private final GspBusinessEntity businessEntity;
    private GspMetadata metadata;
    private String relativePath;
    private String compAssemblyName;

    public BeCommonUtilsGenerator(GspBusinessEntity businessEntity, GspMetadata metadata)
    {
        this.relativePath = metadata.getRelativePath();
        this.compAssemblyName = businessEntity.getGeneratedConfigID().toLowerCase()+".common";
        this.businessEntity = businessEntity;
        this.metadata = metadata;
    }

    public void generateCommonUtils()
    {
        MetadataProjectService service = SpringBeanUtils.getBean(MetadataProjectService.class);
        ProcessMode mode =  service.getProcessMode(relativePath);
        if(mode!=ProcessMode.interpretation)
            return;
        String compModulePath = service.getJavaCompProjectPath(relativePath);
        File folder =new File(compModulePath);
        if(folder.exists()==false)
            return;
        for (GspBizEntityObject entityObject:businessEntity.getAllNodes())
        {
            if(isHasComponent(businessEntity, entityObject)){
                new BeEntityUtilsGenerator(entityObject,compAssemblyName,relativePath).generate();
            }
        }
        BizMgrActionCollection bizMgrAction = businessEntity.getCustomMgrActions();
        if(bizMgrAction != null && bizMgrAction.size() > 0){
            new BeMgrActionUtilsGenerator(businessEntity,compAssemblyName,relativePath).generate();
        }
    }

    private boolean isHasComponent(GspBusinessEntity businessEntity, GspBizEntityObject bizEntityObject){
        if(businessEntity.getCustomMgrActions() != null && businessEntity.getCustomMgrActions().size() > 0)
            return true;
        if(bizEntityObject.getBizCommonDtms() != null && bizEntityObject.getBizCommonDtms().size() > 0)
            return true;
        if(bizEntityObject.getBizCommonVals() != null && bizEntityObject.getBizCommonVals().size() > 0)
            return true;
        if(bizEntityObject.getTccSettings() != null && bizEntityObject.getTccSettings().size() > 0)
            return true;
        return false;
    }
}
